package com.sdtv.basemodule.mvp.presenter;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sdtv.basemodule.mvp.IView;

import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.mvp.presenter
 * @ClassName: PresenterDispatch
 * @Description: presneter-注解-绑定view
 * @Author: 小郭同学
 * @CreateDate: 2019-06-19 16:47
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PresenterDispatch {

    private PresenterProviders mProviders;

    /**
     * 构造器
     *
     * @param presenterProviders
     */
    public PresenterDispatch(PresenterProviders presenterProviders) {
        mProviders = presenterProviders;
    }

    //将presenter和view绑定
    public <P extends BasePresenter> void attachView(Context context, IView view) {
        PresenterStore store = mProviders.getPresenterStore();
        HashMap<String, P> map = store.getMap();
        for (Map.Entry<String, P> entry : map.entrySet()) {
            P presenter = entry.getValue();
            if (null != presenter) {
                presenter.attachView((AppCompatActivity) context, view);
            }
        }
    }


    public <P extends BasePresenter> void onCreatePresenter(@Nullable Bundle savedState) {
        PresenterStore store = mProviders.getPresenterStore();
        HashMap<String, P> mMap = store.getMap();
        for (Map.Entry<String, P> entry : mMap.entrySet()) {
            P presenter = entry.getValue();
            if (presenter != null) {
                presenter.onCreatePresenter(savedState);
            }
        }
    }

    public <P extends BasePresenter> void onDestroyPresenter() {
        PresenterStore store = mProviders.getPresenterStore();
        HashMap<String, P> mMap = store.getMap();
        for (Map.Entry<String, P> entry : mMap.entrySet()) {
            P presenter = entry.getValue();
            if (presenter != null) {
                presenter.onDestroyPresenter();
            }
        }
    }

}
